<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Worksheet extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		// check if user logged in
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');	}
		$this->load->model('user/worksheet_model');
		$this->load->model('founder/all_attendance_model');
		$this->load->library('form_validation');
	}

	function index($id,$year_id,$month_id,$hashtag=NULL)
	{	
		
	//	var_dump();
		$timestamp = mktime(0, 0, 0, $month_id, 10);
		$monthName = date("F", $timestamp);
		$this->data['current_month'] = $monthName;
		$this->data['unfilled_worksheet'] = $this->worksheet_model->unfilled_worksheet($id,$year_id,$month_id);
		$name = $this->data['get_user'] = $this->worksheet_model->get_user($id);		
		$meta['page_title'] = $name['username'].'\'s'.' Worksheet';
		
		if(!isset($hashtag)){
		$this->data['user_id'] = $id;
		$this->data['get_user'] = $this->worksheet_model->get_user($id);
		$this->data['worksheet'] = $this->worksheet_model->get_worksheet($id,$year_id,$month_id);		
		}else{
		$this->data['hashtag'] = $hashtag;
		$this->data['user_id'] = $id;
		$this->data['get_user'] = $this->worksheet_model->get_user($id);
		$this->data['worksheet'] = $this->worksheet_model->get_worksheet_hashtag($id,$year_id,$month_id,$hashtag);
		$this->data['worksheet_more'] = $this->worksheet_model->search_tags($hashtag,$id,$month_id,$year_id);			
		}	
		$this->data['year_lst'] = $this->worksheet_model->get_worksheet_year();	
		echo("Year List");
		echo("<pre>");
		print_r($this->data['year_lst']);
		
		$month_lst = $this->worksheet_model->get_worksheet_months($year_lst->year_id);
		echo("Month List");
		echo("<pre>");
		print_r($month_lst);
		exit();
		$this->data['months'] = $this->worksheet_model->get_worksheet_months();		
		$this->load->view('common/header', $meta);
		$this->load->view('user/worksheet',$this->data);
		$this->load->view('common/footer');
	}
	
	function worksheet_tags($id,$year_id,$month_id,$hashtag=NULL)
	{
		$name = $this->data['get_user'] = $this->worksheet_model->get_user($id);		
		$meta['page_title'] = $name['username'].'\'s'.' Worksheet';
		
		$this->data['hashtag'] = $hashtag;
		$this->data['user_id'] = $id;
		$this->data['get_user'] = $this->worksheet_model->get_user($id);
		$this->data['worksheet'] = $this->worksheet_model->get_worksheet_hashtag($id,$year_id,$hashtag);
		//$this->data['worksheet_more'] = $this->worksheet_model->search_worksheet_tags($hashtag,$id,$month_id,$year_id);				
		$this->data['tagnames'] = $this->worksheet_model->get_all_tagnames();			
		$this->data['months'] = $this->worksheet_model->get_worksheet_months();
					
		$this->load->view('common/header', $meta);
		$this->load->view('user/worksheet-tags',$this->data);
		$this->load->view('common/footer');
	}
	
	
	function insert_multiple_worksheet(){
		$input = $this->input->post();	
		$user = $input['users_ids'];
								
		$count = count($input['date']);		
		for($i=0;$i<$count;$i++){
			
			//$d=($input['date']);
			$date = explode('-',$input['date'][$i]);
			//print_r($d); 
			//exit;
			$y_id = trim($date[0],'20');
			if(!empty($y_id))
			{
			$m_id = $date[1];
			$d_id = $date[2];
			}
			
			if($y_id==date('y') && $m_id==date('m') && $d_id==date('d'))
			{
				$edited_flag=0;
			}else{
				$edited_flag=1;
			}
			$hr = $input['time_hr'][$i];
			$min= $input['time_min'][$i];
			$details=$input['task_details'][$i];
			
			$description=$input['task_details'][$i];
									
			preg_match_all("/\#(.*?)\ /", $description, $desc_result);
            foreach ($desc_result[1] as $im){	
if(!empty($im))
			{
			$tags = '#'.$im;
			$tag = strtolower($tags);	
			$this->worksheet_model->insert_hashtag($tag);
			}
}
			
								
			$hash_fields=$this->worksheet_model->get_hash_fields();
			foreach($hash_fields as $fields)
			{
				$find_hash[] = '/'.$fields->tag.'/';
				
				$hash_replace[] = "<a href=".site_url('user/worksheet/worksheet_tags').'/'.$user.'/'.$y_id.'/'.$m_id.'/'.substr($fields->tag, 1).">$fields->tag</a>";
			}
						
 			$desc_new = preg_replace ($find_hash, $hash_replace, $description); 				
						
			if(!empty($details) && !empty($date) && ($hr !=00 || $min !=00))
			{
			if($input['date'][$i]){					
			$insert = array(
			'year_id'     => $y_id,
			'month_id'    => $m_id,
			'date_id'     => $date[2],
			'user_id'     => $user,
			'desc'		  => $desc_new,
			'edited_flag' => $edited_flag,
			'time_spent'  => $input['time_hr'][$i].':'.$input['time_min'][$i],			
			);
			$this->worksheet_model->save_task($insert);
			}
			}
		}
		redirect('/user/worksheet/index/'.$user.'/'.$y_id.'/'.$m_id);
	}
	
	function search_tags($hashtag=NULL){
		$input = $this->input->post();	
		$user = $input['users_ids'];
		$year_id =  $input['year'];
		//$month_id =  $input['month'];
		$date_id =  $input['date'];
		$tagname = $input['tagname'];	
		
		if(!empty($tagname))
		{
		$t = array();
		foreach($tagname as $tagname){
			if(!empty($tagname)){			
			$t[] = $tagname;
			}
		}
		
	
		$a = $this->worksheet_model->search_tags($t,$user,$year_id);
		}
		$name = $this->data['get_user'] = $this->worksheet_model->get_user($user);		
		if(!empty($name['username']))
		{
		$meta['page_title'] = $name['username'].'\'s'.' Worksheet';
		}else{
		$meta['page_title'] = "Worksheet";
		}
		
		$this->data['hashtag'] = $hashtag;
		$this->data['user_id'] = $user;
		$this->data['get_user'] = $this->worksheet_model->get_user($user);
		if(!empty($a)){
		$this->data['worksheet'] = $a;	
		}
		$this->data['tagnames'] = $this->worksheet_model->get_all_tagnames();			
		$this->data['months'] = $this->worksheet_model->get_worksheet_months();
				
		$this->load->view('common/header', $meta);
		$this->load->view('user/worksheet-tags',$this->data);
		$this->load->view('common/footer');
	
	}
	
	
	function delete_worksheet($id,$user_id,$year_id,$month_id)
	{
		$this->worksheet_model->delete_worksheet($id);
		redirect('user/worksheet/index/'.$user_id.'/'.$year_id.'/'.$month_id,'location');
	}
	
	function all_worksheet(){
		
		$meta['page_title'] = "All Worksheet";
		$this->load->view('common/header', $meta);
		$this->load->view('user/all-worksheet');
		$this->load->view('common/footer');	
	}
	
	
	public function get_worksheet($id){
		$data['worksheet'] = $this->worksheet_model->get_details($id);
		$this->load->view('user/edit_worksheet',$data);
	}
	
	public function update_worksheet(){
		$input = $this->input->post();
		$date_id = $input['date'];
		$month_id = $input['month'];	
		$year_id = $input['year'];
		$desc = $input['desc'];
		$user_id = $input['user_id'];
		if($date_id==date('d') && $month_id==date('m') && $year_id==date('y'))
		{
			$edited_flag = 0;
		}else{
			$edited_flag = 1;
		}
		
		$update = array(
		'desc'=> $desc,
		'edited_flag'=>$edited_flag,
		);		
		$this->worksheet_model->update($update,$input['id']);
		redirect('user/worksheet/index/'.$user_id.'/'.$year_id.'/'.$month_id,'location');
	}
	
	function verify_worksheet(){
			
			$user = $this->input->post('user_id');	
			$date = $this->input->post('date_id');
			$month = $this->input->post('month_id');
			$year = $this->input->post('year_id');
			$dbtime_tot = 0;
			
					
			$combine_worksheet = array();
			for($x=0;$x<count($date);$x++)
			{
				$dbtime_tot = 0;
				$w_where = array(
				'year_id' 	=> $year[$x],
				'month_id'	=> $month[$x],
				'user_id'	=> $user[$x],
				'date_id'	=> $date[$x]
				);
				
			$works = $this->worksheet_model->get_all_worksheet_by_date($w_where);
			
			foreach($works as $work){			
					$dbtime_explode = explode(':',$work->time_spent);
		 			$dbtot_hr 		= $dbtime_explode[0]*60*60;
					$dbtot_min		= $dbtime_explode[1]*60;				
					$dbtime_tot 	+= $dbtot_hr + $dbtot_min; 
					} 
				
				// breaking into hours and mins
						$hours 			= floor($dbtime_tot/ 3600);
						$minutes 		= floor(($dbtime_tot / 60) % 60);
						$sumtime[$x] 	= $hours .':'.$minutes.':00';
				
				$combine_worksheet[$x]['user_id'] = $user[$x];
				$combine_worksheet[$x]['date_id'] = $date[$x];
				$combine_worksheet[$x]['month_id'] = $month[$x];
				$combine_worksheet[$x]['year_id'] = $year[$x];
				$combine_worksheet[$x]['total_worktime'] = $sumtime[$x];
				$combine_worksheet[$x]['verify'] = 1;
			}
			
														
			$this->worksheet_model->worksheet_verified($combine_worksheet);
				
			redirect('user/worksheet/index/'.$user[0].'/'.$year[0].'/'.$month[0],'location');
	}	
	
	
	function add_colors(){
		
		$user = $this->input->post('user_id');	
		$date = $this->input->post('date');
		$month = $this->input->post('month');
		$year = $this->input->post('year');
					
		$id=$_REQUEST['id'];
		$color=$_REQUEST['color'];
					
		$this->worksheet_model->add_work_color($id,$color);
		redirect('user/worksheet/index/'.$user[0].'/'.$year[0].'/'.$month[0],'location');
	}
	
	function add_remarks(){
				
		$where     = array(
			'year_id' =>$_REQUEST['year_id'],
			'month_id'=>$_REQUEST['month_id'],
			'user_id' =>$_REQUEST['user_id'],
			'date_id' =>$_REQUEST['date_id'],
			'remark'  =>$_REQUEST['remark']
		);
				
		$this->worksheet_model->add_worksheet_remark($where);
	}	
	
	function delete_remark($id,$user,$month,$year)
	{		
		$this->worksheet_model->delete_remarks($id);	
		redirect('user/worksheet/index/'.$user.'/'.$year.'/'.$month,'location');	
	}

}

?>